from cmdb.models import Idc, ServerGroup, Server
from rest_framework import serializers
from system_config.serializers import CredentialSerializer
class IdcSerializer(serializers.ModelSerializer):
    """
    IDC机房序列化类
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    class Meta:
        model = Idc
        fields = "__all__"
        read_only_fields = ("id",) # 仅用于序列化（只读）字段，反序列化（更新）可不传

class ServerGroupSerializer(serializers.ModelSerializer):
    """
    主机分组序列化类
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    class Meta:
        model = ServerGroup
        fields = "__all__"
        read_only_fields = ("id",)

class ServerSerializer(serializers.ModelSerializer):
    """
    服务器序列化类
    """
    # idc = IdcSerializer() # 一对多
    idc = IdcSerializer(read_only=True)
    server_group = ServerGroupSerializer(many=True, read_only=True)
    credential = CredentialSerializer(read_only=True)
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    expire_datetime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    class Meta:
        model = Server
        fields = "__all__"
        read_only_fields = ("id",)
